// 问卷结果
export interface QuestionnaireResult {
  date: string
  score: number
  riskLevel: string
  answers: number[]
}

// 骨密度检测结果
export interface BoneDensityResult {
  date: string
  type: string
  tScore: number
  zScore: number
  diagnosis: string
}

// 运动记录
export interface ExerciseRecord {
  date: string
  duration: number
  exercises: string[]
  calories: number
}

// 用药提醒
export interface MedicationRecord {
  name: string
  time: string
  frequency: string
}

// 患者数据
interface PatientData {
  questionnaire: QuestionnaireResult | null
  questionnaireHistory: QuestionnaireResult[]
  boneDensity: BoneDensityResult | null
  boneDensityHistory: BoneDensityResult[]
  exercise: ExerciseRecord[]
  medication: MedicationRecord[]
  lastUpdated: string
}

export class LocalStorageService {
  private static readonly STORAGE_KEY = 'patient_data'

  // 获取所有数据
  static getAllData(): PatientData {
    try {
      const data = AppStorage.Get<string>(this.STORAGE_KEY)
      if (data) {
        return JSON.parse(data)
      }
    } catch (error) {
      console.error('获取数据失败:', error)
    }
    return this.getDefaultData()
  }

  // 获取默认数据结构
  private static getDefaultData(): PatientData {
    return {
      questionnaire: null,
      questionnaireHistory: [],
      boneDensity: null,
      boneDensityHistory: [],
      exercise: [],
      medication: [],
      lastUpdated: new Date().toISOString()
    }
  }

  // 保存所有数据
  private static saveData(data: PatientData): void {
    try {
      data.lastUpdated = new Date().toISOString()
      AppStorage.SetAndLink<string>(this.STORAGE_KEY, JSON.stringify(data))
    } catch (error) {
      console.error('保存数据失败:', error)
    }
  }

  // 保存问卷结果
  static saveQuestionnaireResult(result: QuestionnaireResult): void {
    const data = this.getAllData()
    data.questionnaire = result
    data.questionnaireHistory.unshift(result) // 添加到历史记录开头
    this.saveData(data)
  }

  // 获取最新问卷结果
  static getLatestQuestionnaire(): QuestionnaireResult | null {
    return this.getAllData().questionnaire
  }

  // 获取问卷历史
  static getQuestionnaireHistory(): QuestionnaireResult[] {
    return this.getAllData().questionnaireHistory
  }

  // 保存骨密度检测结果
  static saveBoneDensityResult(result: BoneDensityResult): void {
    const data = this.getAllData()
    data.boneDensity = result
    data.boneDensityHistory.unshift(result) // 添加到历史记录开头
    this.saveData(data)
  }

  // 获取最新骨密度结果
  static getLatestBoneDensity(): BoneDensityResult | null {
    return this.getAllData().boneDensity
  }

  // 获取骨密度历史
  static getBoneDensityHistory(): BoneDensityResult[] {
    return this.getAllData().boneDensityHistory
  }

  // 添加运动记录
  static addExerciseRecord(record: ExerciseRecord): void {
    const data = this.getAllData()
    data.exercise.unshift(record) // 添加到开头
    this.saveData(data)
  }

  // 获取所有运动记录
  static getAllExerciseRecords(): ExerciseRecord[] {
    return this.getAllData().exercise
  }

  // 添加用药提醒
  static addMedication(medication: MedicationRecord): void {
    const data = this.getAllData()
    data.medication.push(medication)
    this.saveData(data)
  }

  // 获取所有用药提醒
  static getAllMedications(): MedicationRecord[] {
    return this.getAllData().medication
  }

  // 清除所有数据
  static clearAll(): void {
    try {
      AppStorage.Delete(this.STORAGE_KEY)
    } catch (error) {
      console.error('清除数据失败:', error)
    }
  }
}
